package com.jinwei.qunguanjia.web.user.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.PageDataUtil;
import com.jinwei.qunguanjia.common.utils.PageUtils;
import com.jinwei.qunguanjia.common.utils.SystemContextUtils;
import com.jinwei.qunguanjia.user.dto.UserIntegralDTO;
import com.jinwei.qunguanjia.user.dto.UserDTO;
import com.jinwei.qunguanjia.user.service.UserIntegralService;
import com.jinwei.qunguanjia.user.service.UserService;
import com.jinwei.qunguanjia.web.user.vo.MyIntegralVO;
import com.jinwei.qunguanjia.web.user.vo.UserIntegralVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 积分管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/userIntegral")
public class UserIntegralController {

    @Resource
    private UserIntegralService userIntegralService;

    @Resource
    private UserService userService;

    /**
     * 我的积分接口
     */
    @GetMapping("/listPage")
    public Response<MyIntegralVO> list() throws IllegalAccessException {
        MyIntegralVO myIntegralVO = new MyIntegralVO();
        UserDTO userDTO = userService.get(SystemContextUtils.getUserId());
        myIntegralVO.setMyIntegral(userDTO.getMyIntegral());
        PageDataUtil<UserIntegralDTO, UserIntegralVO> pageDataUtil = new PageDataUtil<>();
        UserIntegralDTO userIntegralDTO = new UserIntegralDTO();
        userIntegralDTO.setUserId(SystemContextUtils.getUserId());
        PageUtils.startPage();
        PageDataInfo<UserIntegralVO> pageDataInfoVO = pageDataUtil.transform(userIntegralService.listPage(userIntegralDTO), UserIntegralVO.class);
        myIntegralVO.setPageDataInfo(pageDataInfoVO);
        return Response.success(myIntegralVO);
    }

    /**
     * 新增积分接口
     */
    @PostMapping("/save")
    public Response save() {
        String userId = SystemContextUtils.getUserId();
        UserIntegralDTO integralDTO = UserIntegralDTO.builder()
                .integralType("0")
                .integralSource("3")
                .integralNumber("10")
                .userId(userId)
                .build();
        userIntegralService.save(integralDTO);
        UserDTO userQueryDTO = userService.get(userId);
        int myIntegral = Integer.parseInt(userQueryDTO.getMyIntegral());
        myIntegral = myIntegral + 10;
        UserDTO userDTO = new UserDTO();
        userDTO.setUserId(userId);
        userDTO.setMyIntegral(String.valueOf(myIntegral));
        userService.update(userDTO);
        return Response.success();
    }
    
}
